<script setup>
import { Search, Refresh, Plus, Delete, Edit } from '@element-plus/icons-vue'
import diaLog from '@/views/admin/components/notedialog.vue'
import { ref } from 'vue'
import { adminnoteinfoGetService } from '@/api/admin/note'
const loading = ref(false)
const dialog = ref()
const noteinfo = ref([])
const noteinfoview = ref([])
//获取数据库数据
const getnoteInfoList = async () => {
  loading.value = true
  const res = await adminnoteinfoGetService()
  const data = res.data.data

  // 按照日期字段排序
  data.sort((a, b) => {
    // 假设日期字段为 date，你需要根据实际情况修改此处的字段名
    const dateA = new Date(a.date)
    const dateB = new Date(b.date)
    return dateB - dateA // 从大到小排序，如果需要从小到大排序，则调换 dateB 和 dateA 的位置
  })

  noteinfo.value = data
  noteinfoview.value = data
  loading.value = false
}
getnoteInfoList()
//重新渲染
const onSuccess = () => {
  getnoteInfoList()
}
//添加操作
const addplantingoutinfo = () => {
  dialog.value.open({})
}
//编辑操作
const onEditUserinfo = (row) => {
  dialog.value.open(row)
  //编辑操作
}
import { adminnoteinfoDleService } from '@/api/admin/note'
//删除操作
const onDletplantingoutputinfo = async (row) => {
  await ElMessageBox.confirm('是否删除该记录?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    center: true
  })
  adminnoteinfoDleService(row.id)
    .then((response) => {
      if (response.data.status == 200) {
        ElMessage.success(response.data.message)
        getnoteInfoList()
      } else {
        ElMessage.error(response.data.message)
      }
    })
    .catch((error) => {
      ElMessage.error(error)
    })
}
//搜索
const keytitle = ref('')
const keydate = ref('')
const keySearch = async () => {
  // 备份原始数据

  //   const originalData = [...noteinfo.value]

  //   // 执行搜索
  noteinfoview.value = noteinfo.value.filter((row) => {
    return (
      (keytitle.value === '' || row.title.includes(keytitle.value)) &&
      (keydate.value === '' || row.date.includes(keydate.value))
    )
  })
}
//重置搜索条件
const resetFilter = () => {
  noteinfoview.value = noteinfo.value
  keytitle.value = ''
  keydate.value = ''
}

//导出数据
const table = ref(null)

const Order = ref(true)
const geOrder = () => {
  //排序开关
  if (Order.value) {
    noteinfoview.value.sort((a, b) => {
      // 假设日期字段为 date，你需要根据实际情况修改此处的字段名
      const dateA = new Date(a.date)
      const dateB = new Date(b.date)
      return dateB - dateA // 从大到小排序，如果需要从小到大排序，则调换 dateB 和 dateA 的位置
    })
  } else {
    noteinfoview.value.sort((a, b) => {
      // 假设日期字段为 date，你需要根据实际情况修改此处的字段名
      const dateA = new Date(a.date)
      const dateB = new Date(b.date)
      return dateA - dateB // 从大到小排序，如果需要从小到大排序，则调换 dateB 和 dateA 的位置
    })
  }
}
</script>

<template>
  <page-container title="用户信息">
    <!-- 头部 -->
    <template #extra>
      <strong>通知标题</strong>
      <el-input
        placeholder="输入关键字"
        style="width: 150px"
        v-model="keytitle"
      /><strong>发布时间</strong>
      <el-date-picker
        v-model="keydate"
        type="date"
        value-format="YYYY-MM-DD"
        :size="size"
        style="width: 130px"
      />

      <el-button :icon="Search" @click="keySearch">搜索</el-button>
      <el-button :icon="Refresh" @click="resetFilter">重置</el-button>
      <el-switch
        v-model="Order"
        active-color="#13ce66"
        inactive-color="#ff4949"
        active-text="时间升序"
        inactive-text="时间降序"
        @change="geOrder"
      ></el-switch>
      <el-button type="primary" plain :icon="Plus" @click="addplantingoutinfo"
        >新增</el-button
      >
    </template>
    <el-table
      style="width: 100%"
      v-loading="loading"
      @selection-change="handleSelectionChange"
      ref="table"
      :data="noteinfoview"
    >
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column prop="title" label="通知标题" width="150" />
      <el-table-column prop="content" label="内容" width="380">
        <template v-slot="{ row }">
          <el-tooltip
            class="item"
            effect="dark"
            content-class="tooltip-content"
            placement="top-start"
          >
            <div class="ellipsis-text">{{ row.content }}</div>
            <template v-slot:content>
              <div>{{ row.content }}</div>
            </template>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="date" label="发布时间" width="150" />
      <el-table-column fixed="right" label="操作" width="140">
        <template #default="{ row, $index }">
          <el-button
            type="primary"
            :icon="Edit"
            size="small"
            circle
            plain
            @click="onEditUserinfo(row, $index)"
          ></el-button>
          <el-button
            type="danger"
            size="small"
            :icon="Delete"
            circle
            plain
            @click="onDletplantingoutputinfo(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <diaLog ref="dialog" @success="onSuccess"></diaLog>
  </page-container>
</template>
<style>
.tooltip-content {
  max-width: 500px;
}
.ellipsis-text {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.disabled-cell {
  color: #ccc;
}
</style>
